package com.myshoutui;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.myshout.Client;
import com.myshout.Follow;
import com.myshout.ImageFetcher;
import com.myshout.Offer;
import com.myshout.Store;
import com.myshout.Thing;
import com.myshout.android.commmon.Utils;
import com.myshout.rest.RestClient;
import com.myshoutui.MyShoutDashboardActivity.SpecialViewHolder;
import com.myshoutui.manager.MyShoutDataManager;

public class MyShoutStoreViewActivity extends Activity implements
		OnClickListener {
	private GridView mSpecialGridView;
	private static LayoutInflater mInflater;
	InputMethodManager inputManager;
	TextView mAllDay;
	TextView mCatogory;
	TextView mSaleItem;
	ImageView mItemImage;
	ImageView mItemShare;
	ImageView mItemLike;
	ImageView mDial;
	ImageView mItemDirections;
	ImageView mItemCartImg;
	TextView mItemNumber;
	TextView mOfferTextDetails;
	TextView mRedemText;
	TextView mAddress;
	MyShoutDataManager app;
	List<Thing> mThingData;
	SpecialsAdapter mThingAdapter = null;
	Hashtable<String, ImageView> htImageViews;
	String phoneNumber;
	ProgressDialog progressDialog;

	Long storeId = null;
	protected static final int MSG_FOLLOW_CREATED = 10;
	protected static final int MSG_FOLLOW_DELETED = 20;
	protected static final int MSG_FOLLOW_ERROR = 40;

	// changes 28/7/2013
	private GridView mSalesItemGridView;
	SalesItemsAdapter mSalesAdapter;
	TextView mNoItems;
    ArrayList<String> mCategroies ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.special_activity_details);
		app = (MyShoutDataManager) getApplicationContext();
		mInflater = LayoutInflater.from(getApplicationContext());
		mSpecialGridView = (GridView) findViewById(R.id.speacial);
		htImageViews = new Hashtable<String, ImageView>();
		if (getIntent() != null) {
			storeId = getIntent().getLongExtra("STORED", 0);
		}
		TelephonyManager tMgr = (TelephonyManager) getApplicationContext()
				.getSystemService(Context.TELEPHONY_SERVICE);
		phoneNumber = tMgr.getLine1Number();

		mAllDay = (TextView) findViewById(R.id.all_day);
		mCatogory = (TextView) findViewById(R.id.catogories);
		mSaleItem = (TextView) findViewById(R.id.sale_items);
		mAllDay.setOnClickListener(this);

		mCatogory.setOnClickListener(this);
		mSaleItem.setOnClickListener(this);
		mThingData = MyShoutDashboardActivity.getThings(storeId);
		mThingAdapter = new SpecialsAdapter(mThingData);
		mSpecialGridView.setAdapter(mThingAdapter);
		mItemImage = (ImageView) findViewById(R.id.item_image); // image top
																// left corner
//		 mItemImage.setBackground(background);
		loadIcon(mItemImage, MyShoutDashboardActivity.selectedStore);
		mItemShare = (ImageView) findViewById(R.id.share); // SHARE image top
															// left corner

		mItemLike = (ImageView) findViewById(R.id.like_image); // like image

		mDial = (ImageView) findViewById(R.id.dial); // like image
		mItemDirections = (ImageView) findViewById(R.id.directions); // call
																		// image
		mItemCartImg = (ImageView) findViewById(R.id.item_cart); // like image

		mItemNumber = (TextView) findViewById(R.id.item_number);
		mOfferTextDetails = (TextView) findViewById(R.id.offer_text);
		mRedemText = (TextView) findViewById(R.id.redem_text);
		mAddress = (TextView) findViewById(R.id.address_txt);
		if(MyShoutDashboardActivity.selectedStore
				.getAddress()!=null){
		mAddress.setText(MyShoutDashboardActivity.selectedStore
				.getAddress());
		}
		mRedemText.setText("Click redem texet"); // redem text
		mOfferTextDetails.setText(R.string.cart_offer_txt); // redem text top
															// right corner
		mItemNumber.setText("3"); // item number top right like 3
		if (app.isFollowing(MyShoutDashboardActivity.selectedStore
				.getGoogleId())) {
			mItemLike.setTag("UnFollow");
			mItemLike.setImageResource(R.drawable.like_red);
			mItemLike.setVisibility(View.VISIBLE);
		} else {
			mItemLike.setTag("Follow");
			mItemLike.setImageResource(R.drawable.like);
			mItemLike.setVisibility(View.VISIBLE);
		}

		mItemImage.setOnClickListener(this);
		mItemShare.setOnClickListener(this);
		mDial.setOnClickListener(this);
		mItemLike.setOnClickListener(this);
		mItemDirections.setOnClickListener(this);

		// changes 28/7/2013

		mSalesItemGridView = (GridView) findViewById(R.id.sales_items_gridview);
		mSalesItemGridView.setAdapter(new SalesItemsAdapter(
				MyShoutDashboardActivity.getOfferForStore(storeId)));
		mNoItems = (TextView) findViewById(R.id.no_items_txt);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

	}

	private class SpecialsAdapter extends BaseAdapter {
		List<Thing> mList = null;

		public SpecialsAdapter(List<Thing> mList2) {
			this.mList = mList2;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			// A ViewHolder keeps references to children views to avoid
			// unneccessary calls
			// to findViewById() on each row.
			SpecialViewHolder holder;

			// When convertView is not null, we can reuse it directly, there is
			// no need
			// to reinflate it. We only inflate a new View when the convertView
			// supplied
			// by ListView is null.
			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.special_detail_singleitem, null);

				// Creates a ViewHolder and store references to the two children
				// views
				// we want to bind data to.
				holder = new SpecialViewHolder();
				holder.itemName = (TextView) convertView
						.findViewById(R.id.item_name);
				holder.itemRestarentName = (TextView) convertView
						.findViewById(R.id.restarent_name);
				holder.itemOffer = (TextView) convertView
						.findViewById(R.id.offer_exp);
				holder.discountOffer = (TextView) convertView
						.findViewById(R.id.discount_offer);
				holder.storeImg = (ImageView) convertView
						.findViewById(R.id.item_src);
				holder.cartIcon = (ImageView) convertView
						.findViewById(R.id.cart_img);
				convertView.setTag(holder);
			} else {
				// Get the ViewHolder back to get fast access to the TextView
				// and the ImageView.
				holder = (SpecialViewHolder) convertView.getTag();
			}
			holder.itemName.setText("speacials"); // item name
			holder.itemName.setText(mList.get(position).getName()); // item name
			holder.itemRestarentName.setText(mList.get(position)
					.getDescription()); // item restarent name
			holder.discountOffer.setText("cccc"); // item discount
			Offer data = MyShoutDashboardActivity.getOfferByThing(
					mList.get(position).getId(), storeId);
			if (data != null) {
				holder.itemOffer.setText(""
						+ Client.getFriendlyTimeDiff(data.getStartDate(),
								data.getEndDate()));
			}// item offer expire

			loadIcon(holder.storeImg, mList.get(position));
			return convertView;
		}
	}

	class SpecialViewHolder {

		TextView itemName;
		TextView itemRestarentName;
		TextView itemOffer;
		TextView discountOffer;
		ImageView storeImg;
		ImageView cartIcon;
	}

	void loadIcon(ImageView imageView, Object s) {
		if (s == null)
			return;
		String url = null;
		long id = 0;
		String imageFile = null;
		/*
		 * if (s.getActiveStatus() == 0) {
		 * imageView.setImageResource(R.drawable.closed); return; }
		 */
		if (s instanceof Thing) {
			url = ((Thing) s).getImageUrl();
			id = ((Thing) s).getId();
			imageFile = ((Thing) s).getImageFile();

		}

		if (s instanceof Store) {
			url = ((Store) s).getImageUrl();
			id = ((Store) s).getId();
			imageFile = ((Store) s).getImageFile();

		}

		Drawable drawable = null;
		Object object = Client.getFromMemCache(url == null ? "" : url);
		if (object != null && object instanceof Drawable) {
			drawable = (Drawable) object;
			imageView.setImageDrawable(drawable);
			return;
		}

		imageView.setImageResource(R.drawable.store_default);

		String viewId = imageView.toString();
		imageView.setTag(url);
		htImageViews.put(viewId, imageView);

		String fileUrl = Utils.getFileUrl(imageFile);
		new ImageFetcher(url, fileUrl, id, viewId, handler).start();
	}

	// Handler that receives messages from other threads
	final Handler handler = new Handler() {

		public void handleMessage(Message message) {

			if (progressDialog != null && progressDialog.isShowing()) {
				progressDialog.dismiss();
				progressDialog = null;
			}

			switch (message.what) {

			case MSG_FOLLOW_CREATED:
				mItemLike.setImageResource(R.drawable.like_red);
				mItemLike.setTag("UnFollow");
				break;
			case MSG_FOLLOW_DELETED:
				mItemLike.setTag("Follow");
				mItemLike.setImageResource(R.drawable.like);
				break;
			case MSG_FOLLOW_ERROR:
				Utils.showMessage(MyShoutStoreViewActivity.this,
						getString(R.string.could_not_follow_)
								+ MyShoutDashboardActivity
										.selectedStore.getName());
				break;

			case Client.MSG_UPDATE_IMAGE:
				Bundle b = message.peekData();
				if (b == null)
					return;

				String viewId = b.getString(Client.VIEW_ID);
				String url = b.getString(Client.URL);

				if (message.obj instanceof Drawable && viewId != null
						&& url != null) {
					Drawable drawable = (Drawable) message.obj;
					ImageView imageView = (ImageView) htImageViews.get(viewId);
					if (drawable != null) {
						Client.putInMemCache(url, drawable);
						if (imageView != null) {
							String myUrl = (String) imageView.getTag();
							if (url.equals(myUrl)) {
								imageView.setImageDrawable(drawable);

							}
						}
					} else {
						if (imageView != null) {
							// imageView.setImageResource(R.drawable.thing);
						}
					}
				}
				break;

			}
		}
	};

	private void createFollow(final Follow follow) {
		final Runnable bgRunner = new Runnable() {
			public void run() {
				Message message = Message.obtain();
				try {
					Gson gson = Client.getGson();
					RestClient.postRawData(Client.followResource,
							gson.toJson(follow));
					message.what = MSG_FOLLOW_CREATED;
				} catch (Exception e) {
					message.what = MSG_FOLLOW_ERROR;
					Log.d("CreateFollow", e.toString());
				} finally {
					handler.sendMessage(message);
				}
			}
		};
		Thread bgThread = new Thread(bgRunner);
		bgThread.start();
	}

	private void deleteFollow(final Follow follow) {
		final Runnable bgRunner = new Runnable() {
			public void run() {
				Message message = Message.obtain();
				try {
					RestClient.delete(Client.unfollowResource(follow
							.getPerson().longValue(), follow.getTarget(),
							follow.getGoogleId()));
					message.what = MSG_FOLLOW_DELETED;
				} catch (Exception e) {
					message.what = MSG_FOLLOW_ERROR;
					Log.d("DeleteFollow", e.toString());
				} finally {
					handler.sendMessage(message);
				}
			}
		};
		Thread bgThread = new Thread(bgRunner);
		bgThread.start();
	}

	public Intent callSendActivity(Thing t) {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");

		StringBuffer sb = new StringBuffer();
		if (t == null) {
			sb.append(
					getString(R.string.thought_you_may_like_)
							+ MyShoutDashboardActivity.selectedStore
									.getName()).append(".\n");
			sb.append(
					getString(R.string.download_myshout_at)
							+ " http://goo.gl/sjxcC ").append("\n");
			sb.append(
					getString(R.string.and_visit_)
							+ " http://m.go2myshout.com/"
							+ app.getUserId()
							+ "/"
							+ MyShoutDashboardActivity.selectedStore
									.getId()).append("\n");
			sb.append(getString(R.string._regards) + " "
					+ app.getUserRealname());
		} else {
			sb.append(
					getString(R.string.hey_thought_you_would_like_this_)
							+ t.getName()
							+ " @ "
							+ MyShoutDashboardActivity.selectedStore
									.getName()).append("\n");
			sb.append(
					getString(R.string.download_myshout_at)
							+ " http://goo.gl/sjxcC ").append("\n");
			sb.append(getString(R.string.and_visit_)
					+ " http://m.go2myshout.com/" + app.getUserId() + "/"
					+ MyShoutDashboardActivity.selectedStore.getId()
					+ "/" + t.getId());
		}

		intent.putExtra(android.content.Intent.EXTRA_SUBJECT,
				getString(R.string.check_out_)
						+ MyShoutDashboardActivity.selectedStore
								.getName());
		intent.putExtra(android.content.Intent.EXTRA_TEXT, sb.toString());
		return Intent.createChooser(intent, getString(R.string.share_via));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.all_day:

			// changes 28/7/2013
			if (mSpecialGridView.getVisibility() == View.VISIBLE) {
				mSalesItemGridView.setVisibility(View.GONE);
			} else {
				mSpecialGridView.setVisibility(View.VISIBLE);
				mSalesItemGridView.setVisibility(View.GONE);
			}
			((TextView) findViewById(R.id.all_day))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.button_active_state));
			((TextView) findViewById(R.id.catogories))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.button_idle_state));
			((TextView) findViewById(R.id.sale_items))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.button_idle_state));
			List<Thing> mdata =  MyShoutDashboardActivity.getThings(storeId);
			mThingData.clear();
			for(Thing t : mdata)
				mThingData.add(t);
			mThingAdapter.notifyDataSetChanged();

			break;

		case R.id.catogories:
			if (mSpecialGridView.getVisibility() == View.VISIBLE) {
				mSalesItemGridView.setVisibility(View.GONE);
			} else {
				mSpecialGridView.setVisibility(View.VISIBLE);
				mSalesItemGridView.setVisibility(View.GONE);
			}
			((TextView) findViewById(R.id.all_day))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.button_idle_state));
			((TextView) findViewById(R.id.catogories))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.button_active_state));
			((TextView) findViewById(R.id.sale_items))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.button_idle_state));
			// changes 28/7/2013
			final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(
					this);
			LayoutInflater inflater = getLayoutInflater();
			final View PopupLayout = inflater.inflate(
					R.layout.catogery_item_list, null);
			// helpBuilder.setView(PopupLayout);

			final AlertDialog helpDialog = helpBuilder.create();
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, 200);
			helpDialog.setView(PopupLayout);
			

			ListView salesList = (ListView) PopupLayout
					.findViewById(R.id.catogery_list);
			try {
				 mCategroies = MyShoutDashboardActivity.getCategoryCount(storeId);
		            if (mCategroies!=null&&mCategroies.size()!=0) 
		            {
		            	salesList.setAdapter(new SlowAdapter(getApplicationContext(),
								mCategroies,0));
		            	helpDialog.show();
					} else {
                        Toast.makeText(getApplicationContext(), "No Categroies", Toast.LENGTH_SHORT).show();
					}
					
			} catch (Exception e) {
				// TODO: handle exception
			}
			 

			salesList.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					List<Thing> data = MyShoutDashboardActivity.getThingByCategory(
							mCategroies.get(arg2), storeId);
					mThingData.clear();
					for(Thing t : data)
						mThingData.add(t);
					mThingAdapter.notifyDataSetChanged();
					Toast.makeText(getApplicationContext(),
							"Clicked pos" + arg2, Toast.LENGTH_SHORT).show();
					helpDialog.dismiss();
				}
			});

			break;
		case R.id.sale_items:

			// changes 28/7/2013
			if (mSalesItemGridView.getVisibility() == View.VISIBLE) {
				mSpecialGridView.setVisibility(View.GONE);
			} else {
				mSpecialGridView.setVisibility(View.GONE);
				mSalesItemGridView.setVisibility(View.VISIBLE);
			}
			((TextView) findViewById(R.id.all_day))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.button_idle_state));
			((TextView) findViewById(R.id.catogories))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.button_idle_state));
			((TextView) findViewById(R.id.sale_items))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.button_active_state));
			break;

		case R.id.item_image:
			Toast.makeText(getApplicationContext(), "Item Image",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.share:
			Toast.makeText(getApplicationContext(), "Item share",
					Toast.LENGTH_SHORT).show();
			startActivity(callSendActivity(null));

			break;
		case R.id.dial:
			Toast.makeText(getApplicationContext(), "Item dail",
					Toast.LENGTH_SHORT).show();

			Intent callIntent = new Intent(Intent.ACTION_VIEW);
			callIntent.setData(Uri.parse("tel:"
					+ MyShoutDashboardActivity.selectedStore
							.getPhoneNumber()));
			startActivity(callIntent);

			break;
		case R.id.directions:
			Toast.makeText(getApplicationContext(), "Item directions",
					Toast.LENGTH_SHORT).show();

			final Intent intent = new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://maps.google.com/maps?"
							+ "&daddr="
							+ MyShoutDashboardActivity.selectedStore
									.getLatitude()
							+ ","
							+ MyShoutDashboardActivity.selectedStore
									.getLongitude()));
			intent.setClassName("com.google.android.apps.maps",
					"com.google.android.maps.MapsActivity");
			startActivity(intent);

			break;
		case R.id.like_image:
			Toast.makeText(getApplicationContext(), "Item like",
					Toast.LENGTH_SHORT).show();

			Follow follow = new Follow();
			follow.setPerson(new BigInteger("" + app.getUserId()));
			follow.setPhone(phoneNumber);
			if (MyShoutDashboardActivity.selectedStore.getId() != null)
				follow.setTarget(MyShoutDashboardActivity.selectedStore
						.getId());
			follow.setGoogleId(MyShoutDashboardActivity.selectedStore
					.getGoogleId());

			progressDialog = ProgressDialog.show(MyShoutStoreViewActivity.this,
					"Updating your Follow List ", null);
			if (mItemLike.getTag().equals("Follow")) {
				app.addFollowing(MyShoutDashboardActivity.selectedStore);
				Utils.showMessage(MyShoutStoreViewActivity.this,
						getString(R.string.you_are_now_following_)
								+ MyShoutDashboardActivity
										.selectedStore.getName());
				createFollow(follow);
				// app.createShortcutOnDesktop(MyShoutDashboardActivity.selectedStore);
			} else {
				app.removeFollowing(MyShoutDashboardActivity
						.selectedStore);
				Utils.showMessage(MyShoutStoreViewActivity.this,
						getString(R.string.stopped_following_)
								+ MyShoutDashboardActivity
										.selectedStore.getName());
				deleteFollow(follow);
			}

			break;
		}
	}

	// changes 28/7/2013
	private class SalesItemsAdapter extends BaseAdapter {
		List<Offer> mOffersInStore = null;

		public SalesItemsAdapter(List<Offer> mFavoriteStoresList) {
			this.mOffersInStore = mFavoriteStoresList;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			// return mFavoriteStoresList.size();
			return mOffersInStore.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			// A ViewHolder keeps references to children views to avoid
			// unneccessary calls
			// to findViewById() on each row.
			ViewHolder holder;

			// When convertView is not null, we can reuse it directly, there is
			// no need
			// to reinflate it. We only inflate a new View when the convertView
			// supplied
			// by ListView is null.
			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.dashboard_special_singleitem, null);

				// Creates a ViewHolder and store references to the two children
				// views
				// we want to bind data to.
				holder = new ViewHolder();
				holder.itemName = (TextView) convertView
						.findViewById(R.id.item_name);
				holder.storeName = (TextView) convertView
						.findViewById(R.id.restarent_name);
				holder.itemOffer = (TextView) convertView
						.findViewById(R.id.offer_exp);
				holder.itemPrice = (TextView) convertView
						.findViewById(R.id.price);
				holder.discountOffer = (TextView) convertView
						.findViewById(R.id.discount_offer);
				holder.storeImg = (ImageView) convertView
						.findViewById(R.id.item_src);
				holder.heartIcon = (ImageView) convertView
						.findViewById(R.id.heart_icon);
				convertView.setTag(holder);
			} else {
				// Get the ViewHolder back to get fast access to the TextView
				// and the ImageView.
				holder = (ViewHolder) convertView.getTag();
			}
			// Bind the data efficiently with the holder.
			// holder.itemName.setText(getStoreName(mStoresList.get(position).getStore()));

			Thing thing = MyShoutDashboardActivity.allThings.get(mOffersInStore
					.get(position).getThing());
			final Store storeData = MyShoutDashboardActivity
					.selectedStore;
			loadIcon(holder.storeImg, thing);

			holder.itemName.setText(thing.getName());
			if (thing.getDescription() != null)
				holder.storeName.setText(thing.getDescription());
			if (storeData != null)
				holder.itemPrice.setText(""
						+ Client.formatMoney(storeData.getCurrency(),
								mOffersInStore.get(position).getPaidValue()));
			// holder.discountOffer.setText();
			holder.itemOffer.setText(""
					+ Client.getFriendlyTimeDiff(mOffersInStore.get(position)
							.getStartDate(), mOffersInStore.get(position)
							.getEndDate()));
			holder.discountOffer.setText(mOffersInStore.get(position)
					.getTitle());
			holder.discountOffer.setBackgroundColor(getResources().getColor(
					R.color.dark_red));
			return convertView;
		}

		private class ViewHolder {
			TextView itemName;
			TextView storeName;
			TextView itemOffer;
			TextView itemPrice;
			TextView discountOffer;
			ImageView storeImg;
			ImageView heartIcon;
		}

	}

	private class SlowAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		ArrayList<String> list = null;
        int pos; // highilte the list item
		public SlowAdapter(Context context, ArrayList<String> list,int pos) {
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.list = list;
			this.pos = pos;
		}

		/**
		 * The number of items in the list is determined by the number of
		 * speeches in our array.
		 * 
		 * @see android.widget.ListAdapter#getCount()
		 */
		public int getCount() {
			return list.size();
		}

		/**
		 * Since the data comes from an array, just returning the index is
		 * sufficent to get at the data. If we were using a more complex data
		 * structure, we would return whatever object represents one row in the
		 * list.
		 * 
		 * @see android.widget.ListAdapter#getItem(int)
		 */
		public Object getItem(int position) {
			return position;
		}

		/**
		 * Use the array index as a unique id.
		 * 
		 * @see android.widget.ListAdapter#getItemId(int)
		 */
		public long getItemId(int position) {
			return position;
		}

		/**
		 * Make a view to hold each row.
		 * 
		 * @see android.widget.ListAdapter#getView(int, android.view.View,
		 *      android.view.ViewGroup)
		 */
		public View getView(int position, View convertView, ViewGroup parent) {
             ViewHolder holder;
			if (convertView == null) {
				convertView =  mInflater.inflate(
						R.layout.categories_singlerow_item, parent, false);
				holder = new ViewHolder();
				holder.itemName = (TextView)convertView.findViewById(R.id.catogery_name_txt);
				holder.number = (TextView)convertView.findViewById(R.id.catogery_number_txt);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if(pos == position)
			{
				convertView.setBackgroundResource(R.color.blue_color);
			}
			holder.itemName.setText(list.get(position));

			return convertView;
		}
		private class ViewHolder {
			TextView itemName;
			TextView number;
		}
	}

}
